package br.upis.pf1.dao.util;

import java.io.Serializable;
import java.lang.reflect.ParameterizedType;
import java.util.List;

import org.hibernate.Session;

public class HibernateGenericDAO<T> implements GenericDAO<T> {

	private Session session;
	private Class<T> classe;
	
	public HibernateGenericDAO(Session session) {
		this
		.session= session;
		this.classe = (Class<T>)((ParameterizedType)getClass().getGenericSuperclass()).getActualTypeArguments()[0];
	}

	@Override
	public Session getSession() {
		return this.session;
	}

	@Override
	public T pesquisarPorId(Serializable id) {
		return (T) this.session.get(classe, id);
	}

	@Override
	public List<T> listarTodos() {
		return this.session.createCriteria(this.classe).list();
	}

	@Override
	public T salvar(T entidade) {
		return (T) this.session.merge(classe);
	}

	@Override
	public void excluir(T entidade) {
		this.session.delete(entidade);
	}

}
